package com.woniuxy.creditsystem.systemmag.controller;

import com.woniuxy.creditsystem.common.bean.CompanyInfo;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.systemmag.service.ICompanyService;
import com.woniuxy.creditsystem.systemmag.util.GetBinaryStream;
import com.woniuxy.creditsystem.systemmag.util.UploadImg;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/com")
public class CompanyController {

    @Resource
    private ICompanyService companyServiceImpl;

    /**
     * 新增数据
     * @Author liaojian
     * */
    @GetMapping(value = "/save",produces = {"application/json;charset=utf-8"})
    public ResultMsg save(CompanyInfo com){
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        companyServiceImpl.save(com);
        return resultMsg;
    }

    /**
     * 上传图片
     * @Author liaojian
     * */
    @RequestMapping(value = "/importImg",produces = {"application/json;charset=utf-8"})
    public ResultMsg importData(@RequestParam("file") MultipartFile file) throws Exception{
        ResultMsg resultMsg = new ResultMsg(200,"上传成功",null);
        String fileName = UploadImg.getImgNameTwo(file);
        resultMsg.setData(fileName);
        return resultMsg;
    }

    /**
     * 获取资源二进制流数据
     * @Author liaojian
     * @Return 资源二进制流数据
     * */
    @RequestMapping(value = "/getData",produces = {"application/json;charset=utf-8"})
    public ResponseEntity<byte[]> download(
            String fileName, @RequestHeader("User-Agent") String userAgent){
        return GetBinaryStream.getStreamTwo(fileName,userAgent);
    }

    /**
     * 查询所有
     * @Author liaojian
     * */
    @RequestMapping(value = "/findAll",produces = {"application/json;charset=utf-8"})
    public ResultMsg findAll(){
        ResultMsg resultMsg = new ResultMsg(200,"noData",null);
        List<CompanyInfo> lists = companyServiceImpl.findAll();
        System.out.println(lists.size());
        if(lists.size() > 0){
            resultMsg.setMsg("haveData");
            resultMsg.setData(lists);
        }
        return resultMsg;
    }

    /**
     * 删除数据
     * @Author liaojian
     * */
    @GetMapping(value = "/delete",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(long id){
        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);
        companyServiceImpl.delete(id);
        return resultMsg;
    }
}
